{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 金融 证券 图谱搭建"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本项目为基于 Python 中金融数据包 `TUSHARE` ，搭建一个金融证券知识图谱。\n",
    "\n",
    "项目以 jupyter notebook 的形式开源，方便感兴趣的朋友一步步复现。\n",
    "整个项目结构如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../pictures/structures.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据获取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据来源：TUSHARE\n",
    "\n",
    "网站：https://tushare.pro/document/2\n",
    "\n",
    "从 `TUSHARE` 的接口文档，对数据进行筛选。找出关联数据，这里我先选择以下**6**类数据。\n",
    "- 股票列表\n",
    "- 上市公司基本信息\n",
    "- 上市公司管理层\n",
    "- 公募基金列表\n",
    "- 公募基金公司\n",
    "- 公募基金持仓数据\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -r ../requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-23T14:06:28.094164Z",
     "start_time": "2020-01-23T14:06:22.711163Z"
    }
   },
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import pandas as pd\n",
    "\n",
    "from datetime import timedelta,datetime\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-23T14:06:30.088167Z",
     "start_time": "2020-01-23T14:06:29.722164Z"
    }
   },
   "outputs": [],
   "source": [
    "# 若 token 失效，可在公众号 `知行并重` 后台回复 `tushare` 获得最新 token\n",
    "ts.set_token('b6113c6733fb23cc9ee1249f5a61b0c0df033b1280d97cb770378df5')\n",
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_path = './data/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取上市公司名录"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "链接：https://tushare.pro/document/2?doc_id=25"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|名称|类型|描述|\n",
    "|--|--|--|\n",
    "|ts_code|str|TS代码|\n",
    "|symbol|str|<font  color=#009688  >**股票代码**</font>|\n",
    "|name|str|<font  color=#009688  >**股票名称**</font>|\n",
    "|area|str|所在地域|\n",
    "|industry|str|<font  color=#009688  >**所属行业**</font>|\n",
    "|fullname|str|股票全称|\n",
    "|enname|str|英文全称|\n",
    "|market|str|市场类型 （主板/中小板/创业板/科创板）|\n",
    "|exchange|str|交易所代码|\n",
    "|curr_type|str|交易货币|\n",
    "|list_status|str|上市状态： L上市 D退市 P暂停上市|\n",
    "|list_date|str|上市日期|\n",
    "|delist_date|str|退市日期|\n",
    "|is_hs|str|是否沪深港通标的，N否 H沪股通 S深股通|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T05:54:58.304405Z",
     "start_time": "2020-01-07T05:54:56.967231Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3767, 14)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns = 'ts_code,symbol,name,area,industry,fullname,enname,market,exchange,curr_type,list_status,list_date,delist_date,is_hs'\n",
    "\n",
    "total_stocks = pro.stock_basic(fields = columns)\n",
    "# total_stocks = pro.query('stock_basic', exchange='', list_status='L', fields= columns)\n",
    "\n",
    "total_stocks.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-24T07:29:39.569149Z",
     "start_time": "2019-12-24T07:29:39.559183Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SZSE    2186\n",
       "SSE     1560\n",
       "Name: exchange, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_stocks.exchange.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-24T07:33:08.354407Z",
     "start_time": "2019-12-24T07:33:08.308450Z"
    }
   },
   "outputs": [],
   "source": [
    "# 保存数据\n",
    "total_stocks.to_csv(data_path + 'company_lsit_all.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-20T05:12:44.072237Z",
     "start_time": "2020-01-20T05:12:44.037230Z"
    }
   },
   "outputs": [],
   "source": [
    "total_stocks = pd.read_csv(data_path + 'company_lsit_all.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-24T07:10:04.332832Z",
     "start_time": "2019-12-24T07:10:04.309894Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "主板     1951\n",
       "中小板     940\n",
       "创业板     787\n",
       "科创板      68\n",
       "Name: market, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_stocks.market.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 上市公司基本信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这部分数据与上面的上市公司名录互补，提取所在省份和城市。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T06:13:41.662359Z",
     "start_time": "2020-01-07T06:13:41.653358Z"
    }
   },
   "source": [
    "|名称|类型|默认显示|描述|\n",
    "|---|---|---|---|\n",
    "|ts_code|str|Y| <font  color=#009688  >**股票代码**</font>|\n",
    "|exchange|str|Y| 交易所代码 ，SSE上交所 SZSE深交所|\n",
    "|chairman|str|Y| 法人代表|\n",
    "|manager |str|Y| 总经理|\n",
    "|secretary| str|Y| 董秘|\n",
    "|reg_capital |float| Y| 注册资本|\n",
    "|setup_date|str|Y| 注册日期|\n",
    "|province|str|Y| <font  color=#009688  >**所在省份**</font>|\n",
    "|city|str|Y|  <font  color=#009688  >**所在城市**</font>|\n",
    "|introduction|str|N| 公司介绍|\n",
    "|website |str|Y| 公司主页|\n",
    "|email| str|Y| 电子邮件|\n",
    "|office|str|N| 办公室|\n",
    "|employees| int|Y| 员工人数|\n",
    "|main_business| str|N| 主要业务及产品|\n",
    "|business_scope|str|N| 经营范围|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T06:29:58.075740Z",
     "start_time": "2020-01-07T06:29:56.810875Z"
    }
   },
   "outputs": [],
   "source": [
    "df_szse =  pro.stock_company(exchange = \"SZSE\")\n",
    "df_sse =  pro.stock_company(exchange = \"SSE\")\n",
    "\n",
    "# 合并 深交所 和 上交所 股票\n",
    "df_company_detail = pd.concat([df_szse,df_sse])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T06:35:23.616406Z",
     "start_time": "2020-01-07T06:35:23.610406Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2262, 12), (1627, 12), (3889, 12))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_szse.shape,df_sse.shape,df_company_detail.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T06:36:49.128301Z",
     "start_time": "2020-01-07T06:36:49.085218Z"
    }
   },
   "outputs": [],
   "source": [
    "# 保存数据\n",
    "df_company_detail.to_csv('./stock_graph/company_detail_all.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-20T08:09:07.723312Z",
     "start_time": "2020-01-20T08:09:07.681301Z"
    }
   },
   "outputs": [],
   "source": [
    "df_company_detail = pd.read_csv('./stock_graph/company_detail_all.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-07T06:36:03.463796Z",
     "start_time": "2020-01-07T06:36:03.442795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <td>300363.SZ</td>\n",
       "      <td>002790.SZ</td>\n",
       "      <td>300493.SZ</td>\n",
       "      <td>002716.SZ</td>\n",
       "      <td>300587.SZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>exchange</th>\n",
       "      <td>SZSE</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>SZSE</td>\n",
       "      <td>SZSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chairman</th>\n",
       "      <td>居年丰</td>\n",
       "      <td>罗远良</td>\n",
       "      <td>郎晓刚</td>\n",
       "      <td>曹永贵</td>\n",
       "      <td>许吉锭</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>manager</th>\n",
       "      <td>居年丰</td>\n",
       "      <td>张剑波</td>\n",
       "      <td>葛琼</td>\n",
       "      <td>曹永贵</td>\n",
       "      <td>许孔斌</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>secretary</th>\n",
       "      <td>陶荣</td>\n",
       "      <td>陈雪峰</td>\n",
       "      <td>庞军</td>\n",
       "      <td>许子军</td>\n",
       "      <td>范薇薇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>reg_capital</th>\n",
       "      <td>54274.8</td>\n",
       "      <td>40960</td>\n",
       "      <td>47706.9</td>\n",
       "      <td>96047.8</td>\n",
       "      <td>18171.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>setup_date</th>\n",
       "      <td>20050707</td>\n",
       "      <td>19990419</td>\n",
       "      <td>20001009</td>\n",
       "      <td>20041108</td>\n",
       "      <td>20031226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>province</th>\n",
       "      <td>重庆</td>\n",
       "      <td>福建</td>\n",
       "      <td>上海</td>\n",
       "      <td>湖南</td>\n",
       "      <td>浙江</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <td>重庆市</td>\n",
       "      <td>厦门市</td>\n",
       "      <td>上海市</td>\n",
       "      <td>郴州市</td>\n",
       "      <td>台州市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>website</th>\n",
       "      <td>www.porton.cn</td>\n",
       "      <td>www.rtplumbing.com</td>\n",
       "      <td>www.fortune-co.com</td>\n",
       "      <td>www.jingui-silver.com</td>\n",
       "      <td>www.tiantie.cn</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>email</th>\n",
       "      <td>porton.db@porton.cn</td>\n",
       "      <td>rtplumbing@rtplumbing.com</td>\n",
       "      <td>investment@fortune-co.com</td>\n",
       "      <td>jinguizq@jingui-silver.com;xuzj@jingui-silver....</td>\n",
       "      <td>tiantie@tiantie.cn;ttzh@tiantie.cn</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>employees</th>\n",
       "      <td>1642</td>\n",
       "      <td>2133</td>\n",
       "      <td>186</td>\n",
       "      <td>605</td>\n",
       "      <td>786</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               0                          1  \\\n",
       "ts_code                300363.SZ                  002790.SZ   \n",
       "exchange                    SZSE                       SZSE   \n",
       "chairman                     居年丰                        罗远良   \n",
       "manager                      居年丰                        张剑波   \n",
       "secretary                     陶荣                        陈雪峰   \n",
       "reg_capital              54274.8                      40960   \n",
       "setup_date              20050707                   19990419   \n",
       "province                      重庆                         福建   \n",
       "city                         重庆市                        厦门市   \n",
       "website            www.porton.cn         www.rtplumbing.com   \n",
       "email        porton.db@porton.cn  rtplumbing@rtplumbing.com   \n",
       "employees                   1642                       2133   \n",
       "\n",
       "                                     2  \\\n",
       "ts_code                      300493.SZ   \n",
       "exchange                          SZSE   \n",
       "chairman                           郎晓刚   \n",
       "manager                             葛琼   \n",
       "secretary                           庞军   \n",
       "reg_capital                    47706.9   \n",
       "setup_date                    20001009   \n",
       "province                            上海   \n",
       "city                               上海市   \n",
       "website             www.fortune-co.com   \n",
       "email        investment@fortune-co.com   \n",
       "employees                          186   \n",
       "\n",
       "                                                             3  \\\n",
       "ts_code                                              002716.SZ   \n",
       "exchange                                                  SZSE   \n",
       "chairman                                                   曹永贵   \n",
       "manager                                                    曹永贵   \n",
       "secretary                                                  许子军   \n",
       "reg_capital                                            96047.8   \n",
       "setup_date                                            20041108   \n",
       "province                                                    湖南   \n",
       "city                                                       郴州市   \n",
       "website                                  www.jingui-silver.com   \n",
       "email        jinguizq@jingui-silver.com;xuzj@jingui-silver....   \n",
       "employees                                                  605   \n",
       "\n",
       "                                              4  \n",
       "ts_code                               300587.SZ  \n",
       "exchange                                   SZSE  \n",
       "chairman                                    许吉锭  \n",
       "manager                                     许孔斌  \n",
       "secretary                                   范薇薇  \n",
       "reg_capital                             18171.3  \n",
       "setup_date                             20031226  \n",
       "province                                     浙江  \n",
       "city                                        台州市  \n",
       "website                          www.tiantie.cn  \n",
       "email        tiantie@tiantie.cn;ttzh@tiantie.cn  \n",
       "employees                                   786  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_company_detail.head().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-08T03:05:18.320908Z",
     "start_time": "2020-01-08T03:05:18.315892Z"
    }
   },
   "outputs": [],
   "source": [
    "share_list = set(df_company_detail.ts_code.tolist())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 上市公司管理层"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|名称|类型|默认显示||描述|\n",
    "|----|----|---|--|\n",
    "|ts_code| str|Y| <font  color=#009688  >**TS股票代码**</font>|\n",
    "|ann_date|str|Y| 公告日期|\n",
    "|name|str|Y| <font  color=#009688  >**姓名**</font>|\n",
    "|gender|str|Y| 性别|\n",
    "|lev|str|Y| 岗位类别|\n",
    "|title| str|Y| 岗位|\n",
    "|edu|str|Y| 学历|\n",
    "|national|str|Y| 国籍|\n",
    "|birthday|str|Y| 出生年月|\n",
    "|begin_date|str|Y| 上任日期|\n",
    "|end_date|str|Y| 离任日期|\n",
    "|resume|str|N| 个人简历|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用户需要2000积分才可以调取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-20T05:21:40.623899Z",
     "start_time": "2020-01-20T05:21:40.617897Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_data(share_list,func,data_path='./stock_graph/',filename='temp.csv'):\n",
    "    for i,comp in enumerate(share_list):\n",
    "        df = func(ts_code=comp)\n",
    "        if i == 0:\n",
    "            df.to_csv(data_path + filename,index=False, mode='a')\n",
    "        else:    \n",
    "            df.to_csv(data_path + filename,index=False, mode='a', header=None)\n",
    "\n",
    "        if i % 50 ==0: \n",
    "            print(comp,'_num_:',i)\n",
    "\n",
    "        # 抱歉，您每分钟最多访问该接口80次\n",
    "        time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-20T08:09:25.799149Z",
     "start_time": "2020-01-20T08:09:25.793148Z"
    }
   },
   "outputs": [],
   "source": [
    "share_list = list(set(df_company_detail['ts_code']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-20T06:39:58.897020Z",
     "start_time": "2020-01-20T05:21:46.012453Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "002024.SZ _num_: 0\n",
      "300094.SZ _num_: 50\n",
      "300619.SZ _num_: 100\n",
      "000622.SZ _num_: 150\n",
      "600650.SH _num_: 200\n",
      "688030.SH _num_: 250\n",
      "002387.SZ _num_: 300\n",
      "603912.SH _num_: 350\n",
      "600489.SH _num_: 400\n",
      "600061.SH _num_: 450\n",
      "002311.SZ _num_: 500\n",
      "600875.SH _num_: 550\n",
      "002957.SZ _num_: 600\n",
      "603637.SH _num_: 650\n",
      "600733.SH _num_: 700\n",
      "600877.SH _num_: 750\n",
      "000613.SZ _num_: 800\n",
      "600418.SH _num_: 850\n",
      "001965.SZ _num_: 900\n",
      "688188.SH _num_: 950\n",
      "603985.SH _num_: 1000\n",
      "002370.SZ _num_: 1050\n",
      "000673.SZ _num_: 1100\n",
      "002901.SZ _num_: 1150\n",
      "002232.SZ _num_: 1200\n",
      "688310.SH _num_: 1250\n",
      "300656.SZ _num_: 1300\n",
      "600009.SH _num_: 1350\n",
      "300749.SZ _num_: 1400\n",
      "600547.SH _num_: 1450\n",
      "600022.SH _num_: 1500\n",
      "002029.SZ _num_: 1550\n",
      "603959.SH _num_: 1600\n",
      "002768.SZ _num_: 1650\n",
      "600051.SH _num_: 1700\n",
      "002244.SZ _num_: 1750\n",
      "300483.SZ _num_: 1800\n",
      "000008.SZ _num_: 1850\n",
      "000627.SZ _num_: 1900\n",
      "002008.SZ _num_: 1950\n",
      "688101.SH _num_: 2000\n",
      "300430.SZ _num_: 2050\n",
      "000597.SZ _num_: 2100\n",
      "603499.SH _num_: 2150\n",
      "600210.SH _num_: 2200\n",
      "603115.SH _num_: 2250\n",
      "603900.SH _num_: 2300\n",
      "300234.SZ _num_: 2350\n",
      "000938.SZ _num_: 2400\n",
      "603859.SH _num_: 2450\n",
      "300219.SZ _num_: 2500\n",
      "600265.SH _num_: 2550\n",
      "300703.SZ _num_: 2600\n",
      "600774.SH _num_: 2650\n",
      "300553.SZ _num_: 2700\n",
      "300254.SZ _num_: 2750\n",
      "002131.SZ _num_: 2800\n",
      "002376.SZ _num_: 2850\n",
      "300031.SZ _num_: 2900\n",
      "000718.SZ _num_: 2950\n",
      "603936.SH _num_: 3000\n",
      "600740.SH _num_: 3050\n",
      "603569.SH _num_: 3100\n",
      "300383.SZ _num_: 3150\n",
      "002488.SZ _num_: 3200\n",
      "002580.SZ _num_: 3250\n",
      "600269.SH _num_: 3300\n",
      "300033.SZ _num_: 3350\n",
      "603607.SH _num_: 3400\n",
      "300518.SZ _num_: 3450\n",
      "002220.SZ _num_: 3500\n",
      "000728.SZ _num_: 3550\n",
      "002091.SZ _num_: 3600\n",
      "002474.SZ _num_: 3650\n",
      "002520.SZ _num_: 3700\n"
     ]
    }
   ],
   "source": [
    "get_data(share_list,func = pro.stk_managers ,data_path = data_path ,filename=f'stk_managers.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 公募基金列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://tushare.pro/document/2?doc_id=19"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|名称|类型|默认显示|描述|\n",
    "|--|--|--|--|\n",
    "|ts_code|str|Y|<font  color=#009688  >**基金代码**</font>|\n",
    "|name|str|Y|简称|\n",
    "|management|str|Y|<font  color=#009688  >**管理人**</font>|\n",
    "|custodian|str|Y|<font  color=#009688  >**托管人**</font>|\n",
    "|fund_type|str|Y|投资类型|\n",
    "|found_date|str|Y|成立日期|\n",
    "|due_date|str|Y|到期日期|\n",
    "|list_date|str|Y|上市时间|\n",
    "|issue_date|str|Y|发行日期|\n",
    "|delist_date|str|Y|退市日期|\n",
    "|issue_amount|float|Y|发行份额(亿)|\n",
    "|m_fee|float|Y|管理费|\n",
    "|c_fee|float|Y|托管费|\n",
    "|duration_year|float|Y|存续期|\n",
    "|p_value|float|Y|面值|\n",
    "|min_amount|float|Y|起点金额(万元)|\n",
    "|exp_return|float|Y|预期收益率|\n",
    "|benchmark|str|Y|业绩比较基准|\n",
    "|status|str|Y|存续状态D摘牌 I发行 L已上市|\n",
    "|invest_type|str|Y|投资风格|\n",
    "|type|str|Y|基金类型|\n",
    "|trustee|str|Y|受托人|\n",
    "|purc_startdate|str|Y|日常申购起始日|\n",
    "|redm_startdate|str|Y|日常赎回起始日|\n",
    "|market|str|Y|E场内O场外|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "描述：获取公募基金数据列表，包括场内和场外基金\n",
    "积分：用户需要1500积分才可以调取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T07:39:50.973614Z",
     "start_time": "2020-01-10T07:39:46.751148Z"
    }
   },
   "outputs": [],
   "source": [
    "df_e = pro.fund_basic(market='E')   # 交易市场: E场内 O场外（默认E）\n",
    "df_o = pro.fund_basic(market='O')   # 交易市场: E场内 O场外（默认E）\n",
    "\n",
    "df_all = pd.concat([df_e,df_o])\n",
    "\n",
    "df_all.to_csv('./stock_graph/fund_basic.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:54:34.549828Z",
     "start_time": "2020-01-10T11:54:34.542835Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10859, 25)"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_all.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:55:06.110267Z",
     "start_time": "2020-01-10T11:55:06.068267Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>name</th>\n",
       "      <th>management</th>\n",
       "      <th>custodian</th>\n",
       "      <th>fund_type</th>\n",
       "      <th>found_date</th>\n",
       "      <th>due_date</th>\n",
       "      <th>list_date</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>delist_date</th>\n",
       "      <th>...</th>\n",
       "      <th>min_amount</th>\n",
       "      <th>exp_return</th>\n",
       "      <th>benchmark</th>\n",
       "      <th>status</th>\n",
       "      <th>invest_type</th>\n",
       "      <th>type</th>\n",
       "      <th>trustee</th>\n",
       "      <th>purc_startdate</th>\n",
       "      <th>redm_startdate</th>\n",
       "      <th>market</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>515070.SH</td>\n",
       "      <td>AI智能</td>\n",
       "      <td>华夏基金</td>\n",
       "      <td>中国银行</td>\n",
       "      <td>股票型</td>\n",
       "      <td>20191209</td>\n",
       "      <td>None</td>\n",
       "      <td>20191224</td>\n",
       "      <td>20191121</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>None</td>\n",
       "      <td>中证人工智能主题指数收益率</td>\n",
       "      <td>L</td>\n",
       "      <td>被动指数型</td>\n",
       "      <td>契约型开放式</td>\n",
       "      <td>None</td>\n",
       "      <td>20191224</td>\n",
       "      <td>20191224</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>515330.SH</td>\n",
       "      <td>天弘300</td>\n",
       "      <td>天弘基金</td>\n",
       "      <td>招商证券</td>\n",
       "      <td>股票型</td>\n",
       "      <td>20191205</td>\n",
       "      <td>None</td>\n",
       "      <td>20191226</td>\n",
       "      <td>20191104</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>None</td>\n",
       "      <td>沪深300指数收益率</td>\n",
       "      <td>L</td>\n",
       "      <td>被动指数型</td>\n",
       "      <td>契约型开放式</td>\n",
       "      <td>None</td>\n",
       "      <td>20191226</td>\n",
       "      <td>20191226</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>515310.SH</td>\n",
       "      <td>添富300</td>\n",
       "      <td>汇添富基金</td>\n",
       "      <td>招商银行</td>\n",
       "      <td>股票型</td>\n",
       "      <td>20191204</td>\n",
       "      <td>None</td>\n",
       "      <td>20191225</td>\n",
       "      <td>20190927</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>None</td>\n",
       "      <td>沪深300指数收益率</td>\n",
       "      <td>L</td>\n",
       "      <td>被动指数型</td>\n",
       "      <td>契约型开放式</td>\n",
       "      <td>None</td>\n",
       "      <td>20191225</td>\n",
       "      <td>20191225</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>501089.SH</td>\n",
       "      <td>消费增强</td>\n",
       "      <td>方正富邦基金</td>\n",
       "      <td>中国民生银行</td>\n",
       "      <td>股票型</td>\n",
       "      <td>20191129</td>\n",
       "      <td>None</td>\n",
       "      <td>20191231</td>\n",
       "      <td>20191008</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0001</td>\n",
       "      <td>None</td>\n",
       "      <td>中证主要消费红利指数收益率*95%+人民币银行活期存款收益率(税后)*5%</td>\n",
       "      <td>I</td>\n",
       "      <td>增强指数型</td>\n",
       "      <td>契约型开放式</td>\n",
       "      <td>None</td>\n",
       "      <td>20191231</td>\n",
       "      <td>20191231</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>515080.SH</td>\n",
       "      <td>N中证红利</td>\n",
       "      <td>招商基金</td>\n",
       "      <td>浦发银行</td>\n",
       "      <td>股票型</td>\n",
       "      <td>20191128</td>\n",
       "      <td>None</td>\n",
       "      <td>20191227</td>\n",
       "      <td>20191021</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>None</td>\n",
       "      <td>中证红利指数收益率</td>\n",
       "      <td>L</td>\n",
       "      <td>被动指数型</td>\n",
       "      <td>契约型开放式</td>\n",
       "      <td>None</td>\n",
       "      <td>20191227</td>\n",
       "      <td>20191227</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code   name management custodian fund_type found_date due_date  \\\n",
       "0  515070.SH   AI智能       华夏基金      中国银行       股票型   20191209     None   \n",
       "1  515330.SH  天弘300       天弘基金      招商证券       股票型   20191205     None   \n",
       "2  515310.SH  添富300      汇添富基金      招商银行       股票型   20191204     None   \n",
       "3  501089.SH   消费增强     方正富邦基金    中国民生银行       股票型   20191129     None   \n",
       "4  515080.SH  N中证红利       招商基金      浦发银行       股票型   20191128     None   \n",
       "\n",
       "  list_date issue_date delist_date  ...  min_amount  exp_return  \\\n",
       "0  20191224   20191121        None  ...      0.1000        None   \n",
       "1  20191226   20191104        None  ...      0.1000        None   \n",
       "2  20191225   20190927        None  ...      0.1000        None   \n",
       "3  20191231   20191008        None  ...      0.0001        None   \n",
       "4  20191227   20191021        None  ...      0.1000        None   \n",
       "\n",
       "                               benchmark  status  invest_type    type trustee  \\\n",
       "0                          中证人工智能主题指数收益率       L        被动指数型  契约型开放式    None   \n",
       "1                             沪深300指数收益率       L        被动指数型  契约型开放式    None   \n",
       "2                             沪深300指数收益率       L        被动指数型  契约型开放式    None   \n",
       "3  中证主要消费红利指数收益率*95%+人民币银行活期存款收益率(税后)*5%       I        增强指数型  契约型开放式    None   \n",
       "4                              中证红利指数收益率       L        被动指数型  契约型开放式    None   \n",
       "\n",
       "  purc_startdate redm_startdate market  \n",
       "0       20191224       20191224      E  \n",
       "1       20191226       20191226      E  \n",
       "2       20191225       20191225      E  \n",
       "3       20191231       20191231      E  \n",
       "4       20191227       20191227      E  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_all.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 公募基金公司\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "描述：获取公募基金管理人列表\n",
    "\n",
    "积分：用户需要1500积分才可以调取，一次可以提取全部数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|名称|类型|默认显示|描述|\n",
    "|--|--|--|--|\n",
    "|name|str|Y|<font  color=#009688  >**基金公司名称**</font>|\n",
    "|shortname|str|Y|简称|\n",
    "|short_enname|str|N|英文缩写|\n",
    "|province|str|Y|省份|\n",
    "|city|str|Y|城市|\n",
    "|address|str|Y|注册地址|\n",
    "|phone|str|Y|电话|\n",
    "|office|str|Y|办公地址|\n",
    "|website|str|Y|公司网址|\n",
    "|chairman|str|Y|法人代表|\n",
    "|manager|str|Y|总经理|\n",
    "|reg_capital|float|Y|注册资本|\n",
    "|setup_date|str|Y|成立日期|\n",
    "|end_date|str|Y|公司终止日期|\n",
    "|employees|float|Y|员工总数|\n",
    "|main_business|str|Y|主要产品及业务|\n",
    "|org_code|str|Y|组织机构代码|\n",
    "|credit_code|str|Y|统一社会信用代码|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:52:39.898518Z",
     "start_time": "2020-01-10T11:52:30.471442Z"
    }
   },
   "outputs": [],
   "source": [
    "df = pro.fund_company()\n",
    "\n",
    "df.to_csv('./stock_graph/fund_company.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:52:41.407278Z",
     "start_time": "2020-01-10T11:52:41.373275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <td>北京广能投资基金管理有限公司</td>\n",
       "      <td>宏源证券股份有限公司</td>\n",
       "      <td>国元证券股份有限公司</td>\n",
       "      <td>广发证券股份有限公司</td>\n",
       "      <td>长江证券股份有限公司</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>shortname</th>\n",
       "      <td>广能基金</td>\n",
       "      <td>宏源证券</td>\n",
       "      <td>国元证券</td>\n",
       "      <td>广发证券</td>\n",
       "      <td>长江证券</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>province</th>\n",
       "      <td>北京</td>\n",
       "      <td>新疆</td>\n",
       "      <td>安徽</td>\n",
       "      <td>广东</td>\n",
       "      <td>湖北</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <td>北京市</td>\n",
       "      <td>乌鲁木齐市</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>广州市</td>\n",
       "      <td>武汉市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>address</th>\n",
       "      <td>北京市朝阳区北四环中路27号院5号楼2712-2715A</td>\n",
       "      <td>新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦</td>\n",
       "      <td>安徽省合肥市梅山路18号</td>\n",
       "      <td>广东省广州市黄埔区中新广州知识城腾飞一街2号618室</td>\n",
       "      <td>湖北省武汉市江汉区新华路特8号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>phone</th>\n",
       "      <td>None</td>\n",
       "      <td>86-991-2301870</td>\n",
       "      <td>86-551-62207323,86-551-62207968</td>\n",
       "      <td>86-20-87555888,86-20-87550565,86-20-87550265</td>\n",
       "      <td>86-27-65799866,86-27-65799856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>office</th>\n",
       "      <td>北京市朝阳区北四环中路27号院5号楼2712-2715A</td>\n",
       "      <td>新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦</td>\n",
       "      <td>安徽省合肥市梅山路18号</td>\n",
       "      <td>广东省广州市天河区天河北路183-187号大都会广场40楼5楼,7楼,8楼,18楼,19楼,...</td>\n",
       "      <td>湖北省武汉市江汉区新华路特8号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>website</th>\n",
       "      <td>www.gnfund.cn</td>\n",
       "      <td>www.hysec.com</td>\n",
       "      <td>www.gyzq.com.cn</td>\n",
       "      <td>www.gf.com.cn</td>\n",
       "      <td>www.cjsc.com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chairman</th>\n",
       "      <td>刘锡潜</td>\n",
       "      <td>冯戎</td>\n",
       "      <td>蔡咏</td>\n",
       "      <td>孙树明</td>\n",
       "      <td>尤习贵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>manager</th>\n",
       "      <td>杨运成</td>\n",
       "      <td>冯戎</td>\n",
       "      <td>俞仕新</td>\n",
       "      <td>林治海</td>\n",
       "      <td>刘元瑞</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>reg_capital</th>\n",
       "      <td>10000</td>\n",
       "      <td>397241</td>\n",
       "      <td>336545</td>\n",
       "      <td>762109</td>\n",
       "      <td>552947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>setup_date</th>\n",
       "      <td>20111031</td>\n",
       "      <td>19930525</td>\n",
       "      <td>19970606</td>\n",
       "      <td>19940121</td>\n",
       "      <td>19970724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>end_date</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>employees</th>\n",
       "      <td>10</td>\n",
       "      <td>5347</td>\n",
       "      <td>3330</td>\n",
       "      <td>12103</td>\n",
       "      <td>6637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>main_business</th>\n",
       "      <td>None</td>\n",
       "      <td>主要业务:代理买卖证券.</td>\n",
       "      <td>主营业务:经纪业务,自营投资业务,投行业务,资产管理业务,基金管理业务,期货业务,境外业务国...</td>\n",
       "      <td>主营业务:证券经纪</td>\n",
       "      <td>主营业务:证券经纪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>org_code</th>\n",
       "      <td>584419680</td>\n",
       "      <td>228593068</td>\n",
       "      <td>731686376</td>\n",
       "      <td>126335439</td>\n",
       "      <td>700821272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>credit_code</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>91340000731686376P</td>\n",
       "      <td>91440000126335439C</td>\n",
       "      <td>91420000700821272A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          0                         1  \\\n",
       "name                         北京广能投资基金管理有限公司                宏源证券股份有限公司   \n",
       "shortname                              广能基金                      宏源证券   \n",
       "province                                 北京                        新疆   \n",
       "city                                    北京市                     乌鲁木齐市   \n",
       "address        北京市朝阳区北四环中路27号院5号楼2712-2715A  新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦   \n",
       "phone                                  None            86-991-2301870   \n",
       "office         北京市朝阳区北四环中路27号院5号楼2712-2715A  新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦   \n",
       "website                       www.gnfund.cn             www.hysec.com   \n",
       "chairman                                刘锡潜                        冯戎   \n",
       "manager                                 杨运成                        冯戎   \n",
       "reg_capital                           10000                    397241   \n",
       "setup_date                         20111031                  19930525   \n",
       "end_date                               None                      None   \n",
       "employees                                10                      5347   \n",
       "main_business                          None              主要业务:代理买卖证券.   \n",
       "org_code                          584419680                 228593068   \n",
       "credit_code                            None                      None   \n",
       "\n",
       "                                                               2  \\\n",
       "name                                                  国元证券股份有限公司   \n",
       "shortname                                                   国元证券   \n",
       "province                                                      安徽   \n",
       "city                                                         合肥市   \n",
       "address                                             安徽省合肥市梅山路18号   \n",
       "phone                            86-551-62207323,86-551-62207968   \n",
       "office                                              安徽省合肥市梅山路18号   \n",
       "website                                          www.gyzq.com.cn   \n",
       "chairman                                                      蔡咏   \n",
       "manager                                                      俞仕新   \n",
       "reg_capital                                               336545   \n",
       "setup_date                                              19970606   \n",
       "end_date                                                    None   \n",
       "employees                                                   3330   \n",
       "main_business  主营业务:经纪业务,自营投资业务,投行业务,资产管理业务,基金管理业务,期货业务,境外业务国...   \n",
       "org_code                                               731686376   \n",
       "credit_code                                   91340000731686376P   \n",
       "\n",
       "                                                               3  \\\n",
       "name                                                  广发证券股份有限公司   \n",
       "shortname                                                   广发证券   \n",
       "province                                                      广东   \n",
       "city                                                         广州市   \n",
       "address                               广东省广州市黄埔区中新广州知识城腾飞一街2号618室   \n",
       "phone               86-20-87555888,86-20-87550565,86-20-87550265   \n",
       "office         广东省广州市天河区天河北路183-187号大都会广场40楼5楼,7楼,8楼,18楼,19楼,...   \n",
       "website                                            www.gf.com.cn   \n",
       "chairman                                                     孙树明   \n",
       "manager                                                      林治海   \n",
       "reg_capital                                               762109   \n",
       "setup_date                                              19940121   \n",
       "end_date                                                    None   \n",
       "employees                                                  12103   \n",
       "main_business                                          主营业务:证券经纪   \n",
       "org_code                                               126335439   \n",
       "credit_code                                   91440000126335439C   \n",
       "\n",
       "                                           4  \n",
       "name                              长江证券股份有限公司  \n",
       "shortname                               长江证券  \n",
       "province                                  湖北  \n",
       "city                                     武汉市  \n",
       "address                      湖北省武汉市江汉区新华路特8号  \n",
       "phone          86-27-65799866,86-27-65799856  \n",
       "office                       湖北省武汉市江汉区新华路特8号  \n",
       "website                         www.cjsc.com  \n",
       "chairman                                 尤习贵  \n",
       "manager                                  刘元瑞  \n",
       "reg_capital                           552947  \n",
       "setup_date                          19970724  \n",
       "end_date                                None  \n",
       "employees                               6637  \n",
       "main_business                      主营业务:证券经纪  \n",
       "org_code                           700821272  \n",
       "credit_code               91420000700821272A  "
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系——公募基金持仓数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取公募基金持仓数据，**季度**更新\n",
    "积分：用户需要至少1000积分才可以调取，"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://tushare.pro/document/2?doc_id=121"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|名称|类型|默认显示|描述|\n",
    "|--|--|--|--|\n",
    "|ts_code| str| Y| <font  color=#009688  >**TS基金代码**</font>|\n",
    "|ann_date|str |Y| 公告日期|\n",
    "|end_date|str |Y| 截止日期|\n",
    "|symbol|str| Y|  <font  color=#009688  >**股票代码**</font>|\n",
    "|mkv |float| Y| 持有股票市值(元)|\n",
    "|amount|float| Y| 持有股票数量（股）|\n",
    "|stk_mkv_ratio| float| Y| 占股票市值比|\n",
    "|stk_float_ratio| float| Y| 占流通股本比例|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:55:53.238567Z",
     "start_time": "2020-01-10T11:55:53.222565Z"
    }
   },
   "outputs": [],
   "source": [
    "fund_list = list(set(df_all.ts_code))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T11:56:05.080448Z",
     "start_time": "2020-01-10T11:56:05.072447Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10859"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(fund_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T13:22:08.948086Z",
     "start_time": "2020-01-10T13:22:08.941089Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['150242.SZ', '002873.OF', '540001.OF', '007371.OF', '161729.OF']"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fund_list[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-10T16:40:27.447912Z",
     "start_time": "2020-01-10T13:22:13.433674Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000227.OF _num_: 0\n",
      "164509.SZ _num_: 50\n",
      "519971.OF _num_: 100\n",
      "001455.OF _num_: 150\n",
      "005191.OF _num_: 200\n",
      "501030.SH _num_: 250\n",
      "519022.OF _num_: 300\n",
      "510100.SH _num_: 350\n",
      "005388.OF _num_: 400\n",
      "582003.OF _num_: 450\n",
      "519702.OF _num_: 500\n",
      "006218.OF _num_: 550\n",
      "004436.OF _num_: 600\n",
      "000085.OF _num_: 650\n",
      "502036.SH _num_: 700\n",
      "007851.OF _num_: 750\n",
      "519611.OF _num_: 800\n",
      "003957.OF _num_: 850\n",
      "519773.OF _num_: 900\n",
      "162416.SZ _num_: 950\n",
      "006522.OF _num_: 1000\n",
      "001105.OF _num_: 1050\n",
      "150218.SZ _num_: 1100\n",
      "001445.OF _num_: 1150\n",
      "002436.OF _num_: 1200\n",
      "310368.OF _num_: 1250\n",
      "150081.SZ _num_: 1300\n",
      "007445.OF _num_: 1350\n",
      "006624.OF _num_: 1400\n",
      "006505.OF _num_: 1450\n",
      "001788.OF _num_: 1500\n",
      "002736.OF _num_: 1550\n",
      "000110.OF _num_: 1600\n",
      "004741.OF _num_: 1650\n",
      "150017.SZ _num_: 1700\n",
      "005833.OF _num_: 1750\n",
      "002039.OF _num_: 1800\n",
      "001893.OF _num_: 1850\n",
      "005447.OF _num_: 1900\n",
      "003228.OF _num_: 1950\n",
      "184722.SZ _num_: 2000\n",
      "161620.OF _num_: 2050\n",
      "006573.OF _num_: 2100\n",
      "002727.OF _num_: 2150\n",
      "582201.OF _num_: 2200\n",
      "007449.OF _num_: 2250\n",
      "001939.OF _num_: 2300\n",
      "000343.OF _num_: 2350\n",
      "002210.OF _num_: 2400\n",
      "007021.OF _num_: 2450\n",
      "007380.OF _num_: 2500\n",
      "002762.OF _num_: 2550\n",
      "400003.OF _num_: 2600\n",
      "002904.OF _num_: 2650\n",
      "007664.OF _num_: 2700\n",
      "002312.OF _num_: 2750\n",
      "003621.OF _num_: 2800\n",
      "001386.OF _num_: 2850\n",
      "005555.OF _num_: 2900\n",
      "002202.OF _num_: 2950\n",
      "002041.OF _num_: 3000\n",
      "005078.OF _num_: 3050\n",
      "003978.OF _num_: 3100\n",
      "006823.OF _num_: 3150\n",
      "001459.OF _num_: 3200\n",
      "007097.OF _num_: 3250\n",
      "006708.OF _num_: 3300\n",
      "519918.OF _num_: 3350\n",
      "159956.SZ _num_: 3400\n",
      "004298.OF _num_: 3450\n",
      "005234.OF _num_: 3500\n",
      "160725.OF _num_: 3550\n",
      "004640.OF _num_: 3600\n",
      "007278.OF _num_: 3650\n",
      "007006.OF _num_: 3700\n",
      "006235.OF _num_: 3750\n",
      "002425.OF _num_: 3800\n",
      "008202.OF _num_: 3850\n",
      "003154.OF _num_: 3900\n",
      "001846.OF _num_: 3950\n",
      "002102.OF _num_: 4000\n",
      "4037 error: 001526.OF\n",
      "007584.OF _num_: 4050\n",
      "006379.OF _num_: 4100\n",
      "007095.OF _num_: 4150\n",
      "000883.OF _num_: 4200\n",
      "002099.OF _num_: 4250\n",
      "005209.OF _num_: 4300\n",
      "450019.OF _num_: 4350\n",
      "006210.OF _num_: 4400\n",
      "002395.OF _num_: 4450\n",
      "110008.OF _num_: 4500\n",
      "006844.OF _num_: 4550\n",
      "004150.OF _num_: 4600\n",
      "100035.OF _num_: 4650\n",
      "150290.SZ _num_: 4700\n",
      "159969.SZ _num_: 4750\n",
      "002043.OF _num_: 4800\n",
      "005711.OF _num_: 4850\n",
      "161035.SZ _num_: 4900\n",
      "007936.OF _num_: 4950\n",
      "160105.SZ _num_: 5000\n",
      "002048.OF _num_: 5050\n",
      "007287.OF _num_: 5100\n",
      "004006.OF _num_: 5150\n",
      "004352.OF _num_: 5200\n",
      "007493.OF _num_: 5250\n",
      "005277.OF _num_: 5300\n",
      "163115.OF _num_: 5350\n",
      "960025.OF _num_: 5400\n",
      "006524.OF _num_: 5450\n",
      "005400.OF _num_: 5500\n",
      "005104.OF _num_: 5550\n",
      "005914.OF _num_: 5600\n",
      "002506.OF _num_: 5650\n",
      "007309.OF _num_: 5700\n",
      "001926.OF _num_: 5750\n",
      "002273.OF _num_: 5800\n",
      "213007.OF _num_: 5850\n",
      "001974.OF _num_: 5900\n",
      "007611.OF _num_: 5950\n",
      "002454.OF _num_: 6000\n",
      "150102.SZ _num_: 6050\n",
      "166401.SZ _num_: 6100\n",
      "007428.OF _num_: 6150\n",
      "290007.OF _num_: 6200\n",
      "6226 error: 166013.OF\n",
      "005293.OF _num_: 6250\n",
      "000656.OF _num_: 6300\n",
      "003639.OF _num_: 6350\n",
      "006019.OF _num_: 6400\n",
      "000640.OF _num_: 6450\n",
      "000621.OF _num_: 6500\n",
      "161727.SZ _num_: 6550\n",
      "005968.OF _num_: 6600\n",
      "004791.OF _num_: 6650\n",
      "001415.OF _num_: 6700\n",
      "000378.OF _num_: 6750\n",
      "000716.OF _num_: 6800\n",
      "002783.OF _num_: 6850\n",
      "003063.OF _num_: 6900\n",
      "002548.OF _num_: 6950\n",
      "000757.OF _num_: 7000\n",
      "169106.SZ _num_: 7050\n",
      "004132.OF _num_: 7100\n",
      "001204.OF _num_: 7150\n",
      "007413.OF _num_: 7200\n",
      "002042.OF _num_: 7250\n",
      "003791.OF _num_: 7300\n",
      "001623.OF _num_: 7350\n",
      "000767.OF _num_: 7400\n",
      "003795.OF _num_: 7450\n",
      "001110.OF _num_: 7500\n",
      "001680.OF _num_: 7550\n",
      "006195.OF _num_: 7600\n",
      "007645.OF _num_: 7650\n",
      "004283.OF _num_: 7700\n",
      "006357.OF _num_: 7750\n",
      "675161.OF _num_: 7800\n",
      "008354.OF _num_: 7850\n",
      "150021.SZ _num_: 7900\n",
      "310338.OF _num_: 7950\n",
      "150069.SZ _num_: 8000\n",
      "501075.SH _num_: 8050\n",
      "519960.OF _num_: 8100\n",
      "007475.OF _num_: 8150\n",
      "005109.OF _num_: 8200\n",
      "005580.OF _num_: 8250\n",
      "000205.OF _num_: 8300\n",
      "660003.OF _num_: 8350\n",
      "006899.OF _num_: 8400\n",
      "000812.OF _num_: 8450\n",
      "004168.OF _num_: 8500\n",
      "471060.OF _num_: 8550\n",
      "000769.OF _num_: 8600\n",
      "004573.OF _num_: 8650\n",
      "006206.OF _num_: 8700\n",
      "229002.OF _num_: 8750\n",
      "005571.OF _num_: 8800\n",
      "002027.OF _num_: 8850\n",
      "006316.OF _num_: 8900\n",
      "005132.OF _num_: 8950\n",
      "004827.OF _num_: 9000\n",
      "002942.OF _num_: 9050\n",
      "002515.OF _num_: 9100\n",
      "002357.OF _num_: 9150\n",
      "008606.OF _num_: 9200\n"
     ]
    }
   ],
   "source": [
    "get_data(fund_list,func = pro.fund_portfolio ,data_path = data_path ,filename=f'fund_portfolio.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建数据模型 \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过对获取的数据进行梳理，明确内部之间的关联关系，确定节点和关系和节点\n",
    "\n",
    "实体(节点)方面\n",
    "1. 省份\n",
    "2. 城市\n",
    "3. 公司(上市公司 \\ 基金管理公司 \\ 基金托管公司)\n",
    "4. 人（上市公司管理层）\n",
    "5. 基金\n",
    "6. 行业\n",
    "\n",
    "关系方面\n",
    "1. 城市 - [IN_PROVINCE] - > 省份 \n",
    "2. 公司(上市公司\\基金管理公司\\ 基金托管公司) - [IN_CITY] - > 城市\n",
    "4. 公司(上市公司) - [HAS_MANGER] - > 人（上市公司管理层）  [6类]\n",
    "5. 公司(上市公司) - [IN_INDUSTRY] - > 行业\n",
    "6. 基金  - [HAS_MANAGEMNET] - > 公司(基金管理公司)\n",
    "6. 基金  - [HAS_CUSTODIAN] - > 公司(基金托管公司)\n",
    "7. 基金  - [IN_PORTFOLIO] ->  公司(上市公司)\n",
    "\n",
    "初步确定数据模型如下。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![shuju](../pictures/screenshot_2.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
